package com.jyxd.web.controller.czform;

import com.jyxd.web.data.czform.CRRTCure;
import com.jyxd.web.data.dto.HttpResult;
import com.jyxd.web.service.czform.CRRTCureService;
import com.jyxd.web.util.HttpCode;
import com.jyxd.web.util.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(tags = "crrt治疗信息")
@Controller
@RequestMapping(value = "/crrtCure")
public class CRRTCureController {

    private static Logger logger = LoggerFactory.getLogger(CRRTCureController.class);

    @Autowired
    private CRRTCureService crrtCureService;

    /**
     * 增加一条CRRT治疗措施
     *
     * @return
     */
    @ApiOperation("增加一条CRRT治疗措施")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult insert (@RequestBody CRRTCure crrtCure) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        crrtCure.setId(UUIDUtil.getUUID());
        boolean flag = crrtCureService.insert(crrtCure);
        if (flag) {
            return new HttpResult(200, "添加成功");
        } else {
            return new HttpResult(200, "添加失败");
        }
    }

    /**
     * 编辑CRRT前评估信息
     *
     * @param map
     * @return
     */
    @ApiOperation("编辑CRRT前评估信息")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit (@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        if (map != null && map.containsKey("id") && map.containsKey("status")) {
            CRRTCure crrtCure = crrtCureService.queryData(map.get("id").toString());
            if (crrtCure != null) {
                crrtCure.setStatus((int) map.get("status"));
                crrtCureService.update(crrtCure);
            } else {
                return json.toString();
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 删除CRRT前评估信息
     *
     * @param map
     * @return
     */
    @ApiOperation("删除CRRT前评估信息")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public String delete (@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        if (map != null && map.containsKey("id")) {
            CRRTCure crrtCure = crrtCureService.queryData(map.get("id").toString());
            if (crrtCure != null) {
                crrtCure.setStatus(- 1);
                crrtCureService.update(crrtCure);
            } else {
                return json.toString();
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 根据主键id查询CRRT前评估信息
     *
     * @param map
     * @return
     */
    @ApiOperation("根据主键id查询CRRT前评估信息")
    @RequestMapping(value = "/queryData", method = RequestMethod.POST)
    @ResponseBody
    public String queryData (@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        if (map != null && map.containsKey("id")) {
            CRRTCure crrtCureCure = crrtCureService.queryData(map.get("id").toString());
            if (crrtCureCure != null) {
                json.put("data", JSONObject.fromObject(crrtCureCure));
            }
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }

    /**
     * 根据条件分页查询CRRT前评估信息列表（也可以不分页）
     *
     * @param map
     * @return
     */
    @ApiOperation("根据条件分页查询CRRT前评估信息列表（也可以不分页）")
    @RequestMapping(value = "/queryList", method = RequestMethod.POST)
    @ResponseBody
    public String queryList (@RequestBody(required = false) Map<String, Object> map) {
        JSONObject json = new JSONObject();
        json.put("code", HttpCode.FAILURE_CODE.getCode());
        json.put("data", new ArrayList<>());
        if (map != null && map.containsKey("start")) {
            int totalCount = crrtCureService.queryNum(map);
            map.put("start", ((int) map.get("start") - 1) * (int) map.get("size"));
            json.put("totalCount", totalCount);
        }
        List<CRRTCure> list = crrtCureService.queryList(map);
        if (list != null && list.size() > 0) {
            json.put("data", JSONArray.fromObject(list));
        }
        json.put("code", HttpCode.OK_CODE.getCode());
        return json.toString();
    }


}
